const xml2js = require('xml2js');
const xmlParser = new xml2js.Parser({explicitArray : false, ignoreAttrs : true, cdata: true});

class XmlUtil {
    constructor() {
    }

    /**
     *
     * @param {int} amount
     * @returns {Promise<void>}
     */
    async xml2json(xmlStr){
        return new Promise((resolve, reject) => {
            xmlParser.parseString(xmlStr, (err, result) => {
                //将返回的结果再次格式化
                if (err){
                    console.log("parse xml result err:", err);
                    reject(err);
                }else {
                    resolve(result);
                }
            });
        });
    }

    entity2xml(entity){
        let builder = new xml2js.Builder();
        let jsonXml = builder.buildObject({xml: entity});

        return jsonXml;
    }

}


let xmlUtil = new XmlUtil();
module.exports = xmlUtil;